<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 挪车码
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Nuoche extends Controller
{
  public function lst(){
    $data = input('post.');
    if(isset($data['keywords']) and !empty($data['keywords'])){
      if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$data['keywords'])) {
        $map['name'] = $data['keywords'];
      } else {
        if ($data['keywords'] > 0 and $data['keywords'] < 11 and $data) 
        {
          $map['id'] = $data['keywords'];
        }
        if (strlen($data['keywords']) == 11) {
          $map['tel'] = $data['keywords'];
        }
        if (strlen($data['keywords']) > 11) {
          $this->error('输入位数有误');
        }
      }
    }
    if (isset($data['carpai']) and !empty($data['carpai'])) {
      $map['carpai'] = $data['carpai'];
    }
    if(isset($data['jihuo_time']) and !empty($data['jihuo_time'])) {
      $min_time = strtotime($data['jihuo_time']);
      $max_time = $min_time + 24 * 60 * 60;
      $map['start_time'] = ['>=',$min_time,'<',$max_time,'or'];
    }
    $nuoche = empty($map) ? Db::name('nuoche')->order('id desc')->paginate(20) : Db::name('nuoche')->where($map)->order('id desc')->paginate($listRows = 20, $simple = false, $config = ['query'=>array('get'=>$data)]);
    $this->assign('nuoche',$nuoche);
    return $this->fetch();
  }

  public function add(){
    if($this->request->isPost()){
      $post = input('post.tiao');
      for($i = 0; $i < (int)$post; $i++){
        Db::name('nuoche')->insert(['sex' => 1]);
      }
      $this->success('添加成功','nuoche/lst');
    }
    return view();
  }

  public function edit(){
    $id = Request()->param('id');
    if($this->request->isPost()){
      $post = input('post.');
      $post['end_time'] = strtotime($post['end_time']);
      if(false == Db::name('nuoche')->where('id','=',$post['id'])->update($post)){
        $this->error('修改失败');
      } else {
        $this->success('修改成功','nuoche/lst');
      }
    } else {
      $info = Db::name('nuoche')->where('id','=',$id)->find();
      if( $info ){
        $this->assign('info',$info);
      } else {
        $this->error('id错误');
      }
      return view();
    }
  }

  public function del(){
    if($this->request->isAjax()) {
      $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      if(false == Db::name('nuoche')->where('id',$id)->delete()) {
        return $this->error('删除失败');
      } else {
        return $this->success('删除成功','admin/nuoche/lst');
      }
    }
  }

  public function jihuo(){
    //获取文件id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if($id > 0) {
      if($this->request->isPost()) {
        //是提交操作
        $post = $this->request->post();
        $type = $post['status'];
        if(false == Db::name('nuoche')->where('id',$id)->update(['type'=>$type])) {
          $this->error('设置失败');
        } else {
          $this->success('设置成功','admin/nuoche/lst');
        }
      }
    } else {
      $this->error('id不正确');
    }
  }
}
?>